#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/JOBX/lib -I/data/Software/mydan/JOBX/private/lib
use strict;
use warnings;
use Time::HiRes qw/time/;
use POSIX;
use Code;
use Logs;
use Util;
use uuid;
use FindBin qw( $RealBin );
use Encode qw(decode encode);
use MIME::Base64;
use Data::Dumper;
use uuid;
use YAML::XS;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    fromuuid => 'uuid',
    touuid => 'uuid',
    logs => 日志对象

=cut

return sub
{
    my %param = @_;

    my ( $db, $fromuuid, $touuid, $deploy, $rollback, $choicenorollback, $starttime, $rename, $logs ) = @param{qw( db fromuuid touuid deploy rollback choicenorollback starttime rename logs )};

    $logs = Logs->new( 'code.expand' ) unless $logs;

    my $rollbackuuid = uuid::get_rollback_uuid( $touuid );

    my $newname = $rename ? "'$rename'" : 'name';
    $db->execute( "insert into task (projectid,uuid,name,`group`,user,slave,status,starttimems,finishtimems,starttime,finishtime,calltype,pid,runtime,variable,reason )  select projectid,'$touuid',$newname,`group`,user,slave,status,starttimems,finishtimems,starttime,finishtime,calltype,pid,runtime,variable,reason from task where uuid='$fromuuid'");

    $db->execute( "insert into task (projectid,uuid,name,`group`,user,slave,status,starttimems,finishtimems,starttime,finishtime,calltype,pid,runtime,variable,reason )  select projectid,'$rollbackuuid',$newname,'_null_',user,'_null_','success',starttimems,finishtimems,starttime,finishtime,calltype,pid,runtime,variable,reason from task where uuid='$fromuuid'") if $choicenorollback;

    my $variable = $db->query( "select variable from task where uuid='$touuid'" );
    my $v = YAML::XS::Load decode_base64( $variable->[0][0] );

    $v->{_appname_} = $rename if defined $v->{_appname_} && $rename;
    $v->{version} = $deploy if defined $v->{version} && $deploy;
    $v->{_rollbackVersion_} = $rollback if defined $v->{_rollbackVersion_} && $rollback;

    my $newvariable = encode_base64( encode('UTF-8', YAML::XS::Dump $v) );
    $db->execute( "update task set variable='$newvariable' where uuid='$touuid'" );
    $db->execute( "update task set variable='$newvariable' where uuid='$rollbackuuid'" ) if $choicenorollback;


    if( $starttime )
    {
        my $starttimestr = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime( $starttime ) );
        my $rt = $db->query( "select runtime from task  where uuid='$touuid'" );
        my $finishtime = $starttime + $rt->[0][0];
        my $finishtimestr = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime( $finishtime ) );

        $db->execute( "update task set starttimems='$starttime',starttime='$starttimestr',finishtimems='$finishtime',finishtime='$finishtimestr' where uuid='$touuid'" );
    }


    my @col = qw( parent_uuid uuid nodelist nodecount starttime finishtime runtime status confirm );
    my $subtask = $db->query( sprintf( "select %s from subtask where parent_uuid='$fromuuid'", join ',', @col ), \@col );

    my $deploy_argv = $deploy ? " --deploy $deploy " : '';
    my $rollback_argv = $rollback ? " --rollback $rollback " : '';

    my $renaem_argv = $rename ? "--rename $rename" : "";
    for my $sub ( @$subtask  )
    {
        $sub->{parent_uuid} = $touuid;
        my $olduuid = $sub->{uuid};
        $sub->{uuid} = uuid->new()->create_str;

        $db->execute( sprintf "insert into subtask ( %s ) values( %s )", join(',', @col), join( ',', map{"'$_'"}map{ $sub->{$_} || ''}@col ) );
        system "$RealBin/../../JOB/debugtools/expandHistory --fromuuid  $olduuid  --touuid $sub->{uuid} $deploy_argv $rollback_argv --starttime $starttime $renaem_argv";
    }
}
